<?php

namespace App\Http\Controllers\Admin\Shop;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Shop\Product;
use App\Models\Shop\Category;
use App\Models\Shop\Brand;
use DB;

class RecycleController extends Controller
{

    function __construct()
    {
        view()->share([
            '_recycle'=>'am-active',
            '_shop'=>'am-in',

            'categories' => Category::with('children')->where('parent_id', 0)->
            orderBy('id', 'desc')->get(),
            'brands' => Brand::orderBy('id', 'desc')->get()
        ]);
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $infos = array('is_onsale', 'is_top', 'is_recommend', 'is_hot', 'is_new');
        $products = Product::with('categories', 'brand')->onlyTrashed()->paginate(8);
        return view('admin.shop.recycle.index', compact('products', 'infos'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {

        Product::where('id',$id)->forceDelete();
        if (is_array($id)) {
            DB::table('category_product')->whereIn('product_id', $id)->delete();
            DB::table('product_galleries')->whereIn('product_id', $id)->delete();
        } else {
            DB::table('category_product')->where('product_id', $id)->delete();
            DB::table('product_galleries')->where('product_id', $id)->delete();
        }
    }

    /***
     * 单个还原
     */
    public function restore_one(Request $request)
    {
        $id = $request->id;
        Product::withTrashed()
            ->where('id', $id)
            ->restore();
    }

    /***
     * 多个还原
     */
    public function restore_all(Request $request)
    {
        $checked_id = $request->checked_id;
//        foreach ($checked_id as $id) {
//            Product::withTrashed()
//                ->where('id', $id)
//                ->restore();
//        }
        Product::withTrashed()
                ->whereIn('id', $checked_id)
                ->restore();
    }

    /***
     * 多选删除
     */
    public function delete_all(Request $request){
        $id = $request->checked_id;
        Product::whereIn('id',$id)->forceDelete();
        if (is_array($id)) {
            DB::table('category_product')->whereIn('product_id', $id)->delete();
            DB::table('product_galleries')->whereIn('product_id', $id)->delete();
        } else {
            DB::table('category_product')->where('product_id', $id)->delete();
            DB::table('product_galleries')->where('product_id', $id)->delete();
        }
    }
}
